﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class ChainApp : IChain
    {
        private string dbname = "Systems";

        public void Add(ChainEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Chain(");
            builder.Append("ChainName,ChainAdress,ChainType,ChainBossName,ChainBossTel,ChainTel,ChainFax,OpenTime,OverTime,IsUse,CreateMan,CreateTime,IsSeeInfo,ChainDefaultOver,ChainSetp");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.ChainName + "',");
            builder.Append("'" + model.ChainAdress + "',");
            builder.Append("" + model.ChainType + ",");
            builder.Append("'" + model.ChainBossName + "',");
            builder.Append("'" + model.ChainBossTel + "',");
            builder.Append("'" + model.ChainTel + "',");
            builder.Append("'" + model.ChainFax + "',");
            builder.Append("'" + model.OpenTime + "',");
            builder.Append("'" + model.OverTime + "',");
            builder.Append("" + model.IsUse + ",");
            builder.Append("" + model.CreateMan + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.IsSeeInfo + ",");
            builder.Append("" + model.ChainDefaultOver + ",");
            builder.Append("" + model.ChainSetp + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int ChainNum)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Chain ");
            builder.Append(" where ChainNum=" + ChainNum);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Chain ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by ChainNum ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "ChainNum", "T_Chain", swhere);
        }

        public ChainEnt GetModel(int ChainNum)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ChainNum,ChainName,ChainAdress,ChainType,ChainBossName,ChainBossTel,ChainTel,ChainFax,OpenTime,OverTime,IsUse,CreateMan,CreateTime,IsSeeInfo,ChainDefaultOver,ChainSetp from T_Chain ");
            builder.Append(" where ChainNum=" + ChainNum);
            ChainEnt ent = new ChainEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.ChainNum = ChainNum;
            if (table.Rows.Count > 0)
            {
                ent.ChainName = table.Rows[0]["ChainName"].ToString();
                ent.ChainAdress = table.Rows[0]["ChainAdress"].ToString();
                if (table.Rows[0]["ChainType"].ToString() != "")
                {
                    ent.ChainType = int.Parse(table.Rows[0]["ChainType"].ToString());
                }
                ent.ChainBossName = table.Rows[0]["ChainBossName"].ToString();
                ent.ChainBossTel = table.Rows[0]["ChainBossTel"].ToString();
                ent.ChainTel = table.Rows[0]["ChainTel"].ToString();
                ent.ChainFax = table.Rows[0]["ChainFax"].ToString();
                if (table.Rows[0]["OpenTime"].ToString() != "")
                {
                    ent.OpenTime = DateTime.Parse(table.Rows[0]["OpenTime"].ToString());
                }
                if (table.Rows[0]["OverTime"].ToString() != "")
                {
                    ent.OverTime = DateTime.Parse(table.Rows[0]["OverTime"].ToString());
                }
                if (table.Rows[0]["IsUse"].ToString() != "")
                {
                    ent.IsUse = int.Parse(table.Rows[0]["IsUse"].ToString());
                }
                if (table.Rows[0]["CreateMan"].ToString() != "")
                {
                    ent.CreateMan = int.Parse(table.Rows[0]["CreateMan"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["IsSeeInfo"].ToString() != "")
                {
                    ent.IsSeeInfo = int.Parse(table.Rows[0]["IsSeeInfo"].ToString());
                }
                if (table.Rows[0]["ChainDefaultOver"].ToString() != "")
                {
                    ent.ChainDefaultOver = int.Parse(table.Rows[0]["ChainDefaultOver"].ToString());
                }
                if (table.Rows[0]["ChainSetp"].ToString() != "")
                {
                    ent.ChainSetp = int.Parse(table.Rows[0]["ChainSetp"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Chain ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "ChainNum");
        }

        public void Update(ChainEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Chain set ");
            builder.Append("ChainName='" + model.ChainName + "',");
            builder.Append("ChainAdress='" + model.ChainAdress + "',");
            builder.Append("ChainType=" + model.ChainType + ",");
            builder.Append("ChainBossName='" + model.ChainBossName + "',");
            builder.Append("ChainBossTel='" + model.ChainBossTel + "',");
            builder.Append("ChainTel='" + model.ChainTel + "',");
            builder.Append("ChainFax='" + model.ChainFax + "',");
            builder.Append("OpenTime='" + model.OpenTime + "',");
            builder.Append("OverTime='" + model.OverTime + "',");
            builder.Append("IsUse=" + model.IsUse + ",");
            builder.Append("CreateMan=" + model.CreateMan + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("IsSeeInfo=" + model.IsSeeInfo + ",");
            builder.Append("ChainDefaultOver=" + model.ChainDefaultOver + ",");
            builder.Append("ChainSetp=" + model.ChainSetp + "");
            builder.Append(" where ChainNum=" + model.ChainNum + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

